package com.example.baseapp.activity

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.baseapp.R
import com.example.baseapp.adpter.FirstLayerAdapter
import com.example.baseapp.entry.KnowledgeTreeBean
import com.example.baseapp.entry.MyFirstSection
import com.example.baseapp.entry.ThreeLayerBean
import com.example.baseapp.topmethod.readJsonStringFromAssetsFile
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_collapsible_recycler_view.*

class CollapsibleRecyclerViewActivity : AppCompatActivity() {
    private val sectionList = mutableListOf<MyFirstSection>()

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_collapsible_recycler_view)

        val json = application.readJsonStringFromAssetsFile("ThreeLayer.json")
        val threeLayerBean = Gson().fromJson<ThreeLayerBean>(json, KnowledgeTreeBean::class.java)

        val triple = threeLayerBean.tripleLevelKnowledgeList
        for (tripleLevelKnowledge in triple) {
            for (doubleLevelKnowledge in tripleLevelKnowledge.doubleLevelKnowledgeList) {
                val headerSection = MyFirstSection(
                    true,
                    tripleLevelKnowledge.knowledgeName
                )
                sectionList.add(headerSection)
                val itemSection =
                    MyFirstSection(doubleLevelKnowledge.knowledgeName, false, doubleLevelKnowledge)
                sectionList.add(itemSection)
            }

        }
        firstRecycle.layoutManager = LinearLayoutManager(this)
        FirstLayerAdapter(sectionList).also {
            it.bindToRecyclerView(firstRecycle)
        }.also {
            it.setOnItemChildClickListener { _, view, position ->
                when (view.id) {
                    R.id.tv_classHourName -> {
                        it.changeExpandItem(position)
                    }
                }
            }
        }

    }
}